package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.exception.RootException;
import io.aircraft.common.page.PageData;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.ContainerDao;
import io.aircraft.modules.wms.dto.ContainerDTO;
import io.aircraft.modules.wms.entity.ContainerEntity;
import io.aircraft.modules.wms.service.ContainerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 托盘信息
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-12-12
 */
@Service
public class ContainerServiceImpl extends CrudServiceImpl<ContainerDao, ContainerEntity, ContainerDTO> implements ContainerService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<ContainerDTO> page(Map<String, Object> params) {
        IPage<ContainerEntity> page = baseDao.selectPage(
                getPage(params, "cont_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, ContainerDTO.class);
    }

    @Override
    public QueryWrapper<ContainerEntity> getWrapper(Map<String, Object> params) {
        String contNo = (String) params.get("contNo");
        String contType = (String) params.get("contType");
        String contDescription = (String) params.get("contDescription");

        QueryWrapper<ContainerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(contType), "cont_type", contType);
        wrapper.like(StringUtils.isNotBlank(contDescription), "cont_description", contDescription);
        return wrapper;
    }

    @Override
    public void save(ContainerDTO dto) {
        //1.新增托盘需重复性校验
        String contNo = dto.getContNo();
        if (StringUtils.isNotBlank(contNo)) {
            ContainerEntity entity = selectByContNo(contNo);
            if (entity != null) {
                throw new RootException("新增托盘已存在!");
            }
        }
        super.save(dto);
    }

    @Override
    public ContainerEntity selectByContNo(String contNo) {
        QueryWrapper<ContainerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        return baseDao.selectOne(wrapper);
    }
}